package com.spirngboot.middleware.middleware_rabbitmq.server.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class TTLQueueRabbitMqConfig {
    private final String exchangeName = "ttl-exchange";

    //创建交换机
    @Bean
    public DirectExchange directTTLExchange() {
        return new DirectExchange(exchangeName, true, true);
    }

    //创建队列
    @Bean
    public Queue ttlQueue() {
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 5000);
        return new Queue("ttl.direct.queue", false, false, true, args);
    }

    @Bean
    public Queue ttlMessageQueue() {
        return new Queue("ttl.message.direct.queue", false, false, true);
    }

    //绑定队列
    @Bean
    public Binding ttlQueueBing() {
        return BindingBuilder.bind(ttlQueue()).to(directTTLExchange()).with("");
    }

    @Bean
    public Binding ttlMessageQueueBing() {
        return BindingBuilder.bind(ttlMessageQueue()).to(directTTLExchange()).with("ttl_message");
    }
}
